package com.jinke.api.modules.app.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jinke.api.modules.app.enums.OrderTaskEnums;
import com.jinke.api.modules.app.mapper.OrderTaskMapper;
import com.jinke.api.modules.app.model.OrderTask;
import com.jinke.api.modules.app.model.OrderTaskDetail;
import com.jinke.api.modules.app.request.BatchCreateLabelOrderRequest;
import com.jinke.api.modules.app.service.IOrderTaskDetailService;
import com.jinke.api.modules.app.service.IOrderTaskService;
import com.jinke.api.security.util.AdminUserUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 订单定时任务ServiceImpl
 *
 * @author huang
 * @data 2024-04-26
 */
@Slf4j
@Service
public class OrderTaskServiceImpl extends ServiceImpl<OrderTaskMapper, OrderTask> implements IOrderTaskService {
    @Autowired
    private IOrderTaskDetailService iOrderTaskDetailService;

    /**
     * 按组预估版本才有
     * @param request
     */
    @Override
    public void insertTask(List<BatchCreateLabelOrderRequest> request,Integer userId) {
        List<Integer> orderIds = request.stream().map(BatchCreateLabelOrderRequest::getOrderId).collect(Collectors.toList());
        Map<Integer, Integer> integerMap = request.stream()
                .collect(Collectors.toMap(BatchCreateLabelOrderRequest::getOrderId, BatchCreateLabelOrderRequest::getChannelId));

        //查询是否重复下单
        List<OrderTask> orderTasks = lambdaQuery()
                .eq(OrderTask::getUserId, userId)
                .eq(OrderTask::getStatus, OrderTaskEnums.NOT_START)
                .list();
        if (CollectionUtil.isNotEmpty(orderTasks)) {
            List<OrderTaskDetail> orderTaskDetails = iOrderTaskDetailService.lambdaQuery()
                    .isNull(OrderTaskDetail::getStatus)
                    .in(OrderTaskDetail::getOrderTaskId, orderTasks.stream().map(OrderTask::getId).collect(Collectors.toList()))
                    .list();
            if (CollectionUtil.isNotEmpty(orderTaskDetails)) {
                List<Integer> ids = orderTaskDetails.stream().map(OrderTaskDetail::getOrderId).collect(Collectors.toList());
                orderIds = orderIds.stream().filter(o -> !ids.contains(o)
                ).collect(Collectors.toList());
            }
        }
        if (CollectionUtil.isNotEmpty(orderIds)) {
            OrderTask orderTask = new OrderTask();
            orderTask.setUserId(userId);
            orderTask.setCreateBy(AdminUserUtil.getUserInfo().getUsername());
            orderTask.setUpdateBy(AdminUserUtil.getUserInfo().getUsername());
            save(orderTask);

            //channel和订单号一一对应
            List<OrderTaskDetail> orderTaskDetails = orderIds.stream().map(o -> {
                OrderTaskDetail orderTaskDetail = new OrderTaskDetail();
                orderTaskDetail.setOrderId(o);
                orderTaskDetail.setOrderTaskId(orderTask.getId());
                orderTaskDetail.setChannelId(integerMap.get(o));
                return orderTaskDetail;
            }).collect(Collectors.toList());
            iOrderTaskDetailService.saveBatch(orderTaskDetails);
        }
    }




    @Override
    public void insert(BatchCreateLabelOrderRequest request) {
        List<Integer> orderIds = request.getOrderIds();
        //查询是否重复下单
        List<OrderTask> orderTasks = lambdaQuery()
                .eq(OrderTask::getUserId, request.getUserId())
                .eq(OrderTask::getStatus, OrderTaskEnums.NOT_START)
                .list();
        if (CollectionUtil.isNotEmpty(orderTasks)) {
            List<OrderTaskDetail> orderTaskDetails = iOrderTaskDetailService.lambdaQuery()
                    .isNull(OrderTaskDetail::getStatus)
                    .in(OrderTaskDetail::getOrderTaskId, orderTasks.stream().map(OrderTask::getId).collect(Collectors.toList()))
                    .list();
            if (CollectionUtil.isNotEmpty(orderTaskDetails)) {
                List<Integer> ids = orderTaskDetails.stream().map(OrderTaskDetail::getOrderId).collect(Collectors.toList());
                orderIds = request.getOrderIds().stream().filter(o -> !ids.contains(o)
                ).collect(Collectors.toList());
            }
        }
        if (CollectionUtil.isNotEmpty(orderIds)) {
            OrderTask orderTask = new OrderTask();
            orderTask.setUserId(request.getUserId());
            orderTask.setCreateBy(AdminUserUtil.getUserInfo().getUsername());
            orderTask.setUpdateBy(AdminUserUtil.getUserInfo().getUsername());
            save(orderTask);

            List<OrderTaskDetail> orderTaskDetails = orderIds.stream().map(o -> {
                OrderTaskDetail orderTaskDetail = new OrderTaskDetail();
                orderTaskDetail.setOrderId(o);
                orderTaskDetail.setOrderTaskId(orderTask.getId());
                orderTaskDetail.setChannelId(request.getChannelId());
                return orderTaskDetail;
            }).collect(Collectors.toList());
            iOrderTaskDetailService.saveBatch(orderTaskDetails);
        }
    }
}
